package xiaomeng.com.xsngame;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MyView  extends View implements  Runnable {
    Context context;
    //地板类
    Ground ground;

    //管子类
    Column column1;
    Column column2;

    //鸟类
    Bird bird;
    //画笔
    Paint paint=new Paint();
    int startButtonX;
    int startButtonY;
    int stopButtonX;
    int stopButtonY;
    Thread thread=new Thread(this);

    //判断游戏是否开始
    boolean isstart=false;
    //判断是否死亡
    boolean isFail=false;

    //分数
    int fs=0;

    MusicUtil m=new MusicUtil();

    MyDatabaseHelper myDatabaseHelper;
    public MyView(Context context) {
        super(context);
        this.context=context;
        startButtonX=MyApplication.width / 2 - DataImage.start.getWidth() / 2;
        startButtonY=MyApplication.height / 2 - DataImage.start.getHeight() / 2;
        stopButtonX=MyApplication.width / 2 - DataImage.start.getWidth() / 2;
        stopButtonY=MyApplication.height / 2 - DataImage.start.getHeight() / 2+200;
        thread.start();
        myDatabaseHelper=new MyDatabaseHelper(context);
        init();
    }

    //初始化方法
    public  void init(){
       ground=new Ground();
       column1=new Column(MyApplication.width+200);
       column2=new Column(MyApplication.width+200+MyApplication.width/2+50);
       bird=new Bird(140,MyApplication.height/2-30);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setBackgroundResource(R.mipmap.background);

        //绘制开始按钮
        if (isstart==false) {
            canvas.drawBitmap(DataImage.getready,startButtonX-DataImage.getready.getWidth()/4,startButtonY-200,paint);
            canvas.drawBitmap(DataImage.start, startButtonX,startButtonY , paint);
        }
        //绘制水管，和鸟
        if (isstart){
            column1.paint(canvas,paint);
            column2.paint(canvas,paint);
            bird.paint(canvas,paint);

        }
        //绘制地面
        ground.paint(canvas,paint);
        //结束画面
        if (isFail){
            canvas.drawText("当前得分 : "+fs,stopButtonX-60,stopButtonY-20,paint);
            canvas.drawBitmap(DataImage.restart,stopButtonX,stopButtonY,paint);

            paint.setColor(Color.RED);
            paint.setTextSize(60);
            canvas.drawText("排名         得分",stopButtonX-60,300,paint);
            List<Integer> list=select();
            Integer[] sco= new Integer[select().size()];
            list.toArray(sco);

            for (int i=0;i<sco.length-1;i++){
                for (int l=0;l<sco.length-i-1;l++){
                    if (sco[l]<sco[l+1]) {
                        int temp=sco[l];
                        sco[l]=sco[l+1];
                        sco[l+1]=temp;
                    }
                }

            }
            int k=390;
            int j=1;
            for (Integer a:sco){
                if (j<=3) {

                    canvas.drawText(j + "                "+a, stopButtonX - 40, k, paint);
                    j++;
                    k += 60;
                }
            }
        }
        paint.setColor(Color.RED);
        paint.setTextSize(40);
        canvas.drawText("分数 ："+fs,20,50,paint);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x=event.getX();
        float y=event.getY();
        //开始按钮的点击事件
        if (isstart==false&&x>startButtonX&&x<startButtonX+ DataImage.start.getWidth()&&y>startButtonY&&y<startButtonY+ DataImage.start.getHeight()){
            isstart=true;
        }
       //结束按钮的点击事件
        if (isFail&&x>stopButtonX&&x<stopButtonX+ DataImage.restart.getWidth()&&y>stopButtonY&&y<stopButtonY+ DataImage.restart.getHeight()){
            init();
            isFail=false;
            fs=0;
        }
        //小鸟向上飞
        bird.flappy();


        //播放音效
       if (isstart&&isFail==false){
           m.wing();
       }


        return true;
    }



    public void  inisert(){
        SQLiteDatabase sqLiteOpenHelper=myDatabaseHelper.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("fraction",fs);
        sqLiteOpenHelper.insert("data",null,contentValues);
        sqLiteOpenHelper.close();
        Log.i("TGA","插入成功");

    }
    //查询数据
    public List<Integer> select(){
        List<Integer> list=new ArrayList<>();
        SQLiteDatabase sqLiteOpenHelper=myDatabaseHelper.getWritableDatabase();
        Cursor cursor=sqLiteOpenHelper.query("data",null,null,null,null,null,null);
        while (cursor.moveToNext()){
                int fs = cursor.getInt(cursor.getColumnIndex("fraction"));
                list.add(fs);

        }
        return  list;
    }



    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           //移动方法
            if (isstart&&isFail==false) {
                ground.move();
                column1.move();
                column2.move();
                bird.move();
                //碰撞检测
                if (bird.hit(column1,column2,ground)){
                    inisert();
                    isFail=true;
                    m.hit();//播放死亡音效
                }

                if (bird.pass(column1,column2)){
                    fs++;
                }
                postInvalidate();
            }
        }
    }
}
